<?php

  //Include required files
  require_once('inc/addons.php');
  require_once('inc/users.php');
  require_once('inc/functions.php');

  define('SUBPAGE', 'register');

  //Start session
  Users::startSession();

  //check if data is submitted
  if (isset($_POST['submit'])) {
    //check name
    if (strlen($_POST['name']) == 0) {
      $errors[] = 'Username must be entered.';
    } else if (Users::checkExists($_POST['name'])) {
      $errors[] = 'Username already exists.';
    } else if (!ctype_alnum($_POST['name'])) {
      $errors[] = 'Username must be alphanumeric';
    }

    //check email
    if (strlen($_POST['email']) == 0) {
      $errors[] = 'E-mail address must be entered.';
    } else {
      if (!preg_match('/^[^@]+@[^@]+\.[a-z]+$/i', $_POST['email'])) {
        $errors[] = 'Invalid email.';
      }
    }
    //check password
    if (strlen($_POST['password']) < 6) {
      $errors[] = 'Password must be 6 or more characters.';
    } 

    //if no errors register the user
    if (!isset($errors)) {
      if (Users::addUser($_POST['name'], $_POST['password'], $_POST['email'])) {
        Users::logIn($_POST['name'], $_POST['password']);
        header('Location: /account');
        die();
      } else {
        $errors[] = 'Adding Failed: '.Users::getLastError();
      }
    }
  }

  //Show the dmdirc header
  showHeader();

  //Check if the user is logged in
  if (Users::isLoggedIn()) {
    echo '<h2>Registration error</h2>';
    echo '<p>You already have an account, you cannot create another.</p>';
    showFooter();
    die();
  }
?>

  <h2>Registration</h2>
  <p>To register please fill in the following information.</p>
  <?php
    //Check if there were any errors and output
    if (isset($errors)) {
      echo '<p style="color: #ff0000">Please correct the following errors with your registration.</p>';
      echo '<ol style="color: #ff0000">';
      foreach ($errors as $error) {
        echo '<li>'.$error.'</li>';
      }
      echo '</ol>';
    }
    //output the registration form 
  ?>
  <form action="/register" method="post">
    <table>
      <tr>
        <td>
          <label for="name">Username: </label>
        </td>
        <td>
          <input type="text" id="name" name="name" 
                 value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>" 
          />
        </td>
      </tr>
      <tr>
        <td>
          <label for="email">E-Mail: </label>
        </td>
        <td>
          <input type="text" id="email" name="email" 
                 value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>" 
          />
        </td>
      </tr>
      <tr>
        <td>
          <label for="password">Password: </label>
        </td>
        <td>
          <input type="password" id="password" name="password" 
                 value="<?php echo isset($_POST['password']) ? $_POST['password'] : ''; ?>" 
          />
        </td>
      </tr>
      <tr>
        <td></td>
        <td>
          <input type="submit" id="submit" name="submit" value="Register" />
          <input type="reset" />
        </td>
      </tr>
    </table>
  </form>

<?php
  //Show the dmdirc footer
  showFooter();
?>
